package com.example.moshudriver.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.ORDER_INFO;
import com.example.moshudriver.protocol.orderacceptRequest;
import com.example.moshudriver.protocol.orderacceptResponse;
import com.example.moshudriver.protocol.ordercancelRequest;
import com.example.moshudriver.protocol.ordercancelResponse;
import com.example.moshudriver.protocol.orderconfirm_payRequest;
import com.example.moshudriver.protocol.orderconfirm_payResponse;
import com.example.moshudriver.protocol.orderinfoRequest;
import com.example.moshudriver.protocol.orderinfoResponse;
import com.example.moshudriver.protocol.orderpayRequest;
import com.example.moshudriver.protocol.orderpayResponse;
import com.example.moshudriver.protocol.orderwork_doneRequest;
import com.example.moshudriver.protocol.orderwork_doneResponse;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

public class OrderInfoModel extends BaseModel {

	public ORDER_INFO publicOrder;
	public String offer_distance;
	public String transaction_distance;

	public OrderInfoModel(Context context) {
		super(context);
	}

	public void get(int orderId) {
		orderinfoRequest request = new orderinfoRequest();
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						orderinfoResponse response = new orderinfoResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							publicOrder = response.order_info;
							OrderInfoModel.this.OnMessageResponse(url, jo,
									status);
						} else {
							OrderInfoModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						OrderInfoModel.this.OnMessageResponse(url, jo, status);
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_INFO).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}

	/**
	 * 取消订单
	 * 
	 * @param orderId
	 */
	public void cancel(int orderId) {
		ordercancelRequest request = new ordercancelRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						ordercancelResponse response = new ordercancelResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							publicOrder = response.order_info;
							OrderInfoModel.this.OnMessageResponse(url, jo,
									status);
						} else {
							OrderInfoModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_CANCEL).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}

	/**
	 * 接单
	 * 
	 * @param orderId
	 */
	public void accept(int orderId) {
		orderacceptRequest request = new orderacceptRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						orderacceptResponse response = new orderacceptResponse();
						response.fromJson(jo);
						if (response.order_info != null) {
							publicOrder = response.order_info;
						}
						OrderInfoModel.this.OnMessageResponse(url, jo, status);
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_ACCEPT).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}

	/**
	 * 订单完成
	 * 
	 * @param orderId
	 * @param transaction_price
	 */
	public void done(int orderId, String transaction_price,
			String transaction_distance) {
		orderwork_doneRequest request = new orderwork_doneRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;
		request.transaction_price = transaction_price;
		request.transaction_distance = transaction_distance;
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						orderwork_doneResponse response = new orderwork_doneResponse();
						response.fromJson(jo);
						
						if (response.succeed == 1) {
							publicOrder = response.order_info;
							OrderInfoModel.this.OnMessageResponse(url, jo,
									status);
						} else {
							OrderInfoModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject jo = request.toJson();
			if (transaction_price.equals("")) {
				jo.remove("transaction_price");
			}
			params.put("json", jo.toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_WORK_DONE).type(JSONObject.class)
				.params(params);
		ajaxProgress(cb);
	}

	/**
	 * 支付订单
	 * 
	 * @param orderId
	 * @param payCode
	 *            支付方式
	 */
	public void pay(int orderId, int payCode) {
		orderpayRequest request = new orderpayRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;
		request.pay_code = payCode;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						orderpayResponse response = new orderpayResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							publicOrder = response.order_info;
							OrderInfoModel.this.OnMessageResponse(url, jo,
									status);
						} else {
							OrderInfoModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_PAY).type(JSONObject.class).params(params);
		ajaxProgress(cb);
	}

	/**
	 * 确认支付
	 * 
	 * @param orderId
	 */
	public void confirmPay(int orderId) {
		orderconfirm_payRequest request = new orderconfirm_payRequest();
		request.uid = SESSION.getInstance().uid;
		request.sid = SESSION.getInstance().sid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.order_id = orderId;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					OrderInfoModel.this.callback(this, url, jo, status);
					if (null != jo) {
						orderconfirm_payResponse response = new orderconfirm_payResponse();
						response.fromJson(jo);
						if (response.succeed == 1) {
							publicOrder = response.order_info;
							OrderInfoModel.this.OnMessageResponse(url, jo,
									status);
						} else {
							OrderInfoModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					}
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		cb.url(ApiInterface.ORDER_CONFIRM_PAY).type(JSONObject.class)
				.params(params);
		ajaxProgress(cb);
	}

}
